﻿layui.define(['jquery'], function (exports) {
    var $ = layui.jquery;
    var Notice = function (message, type, options) {
        var defaultOptions = {
            appendTo: "body",
            stack: true,
            position_class: "notice-top-right",
            fullscreen: false,
            width: 350,
            spacing: 15,
            timeout: 3000,
            has_close_btn: true,
            has_icon: true,
            sticky: false,
            border_radius: 6,
            has_progress: true,
        }

        var $element = null;

        var $options = $.extend(true, {}, defaultOptions, options);

        var spacing = $options.spacing;

        var css = {
            "position": ($options.appendTo == "body") ? "fixed" : "absolute",
            "max-width": $options.width <= 0 ? 350 : $options.width,
            "display": "none",
            "border-radius": $options.border_radius,
            "z-index": 99999
        }

        $element = $('<div class="notice-item-wrapper ' + type + ' ' + $options.position_class + '"></div>');

        $('<div class="notice-message">' + message + '</p>').appendTo($element);

        if ($options.fullscreen) {
            $element.addClass("fullscreen");
        }


        if ($options.has_close_btn) {
            $('<div class="notice-close">&times;</div>').appendTo($element);
            css["padding-right"] = 20;
        }

        if ($options.has_icon) {
            $('<i class="notice-icon iconfont icon-' + type + '"></i>').appendTo($element);
            css["padding-left"] = 50;
        }

        if ($options.has_progress && $options.timeout > 0) {
            $('<div class="notice-progress ' + type + '"></div>').appendTo($element);
        }

        if ($options.sticky) {
            $options.spacing = 0;
            spacing = 0;

            switch ($options.position_class) {
                case "notice-top-left": {
                    css["top"] = 0;
                    css["left"] = 0;
                    break;
                }
                case "notice-top-right": {
                    css["top"] = 0;
                    css["left"] = 0;
                    break;
                }
                case "notice-top-center": {
                    css["top"] = 0;
                    css["left"] = css["right"] = 0;
                    css["width"] = "100%";
                    break;
                }
                case "notice-bottom-left": {
                    css["bottom"] = 0;
                    css["left"] = 0;
                    break;
                }
                case "notice-bottom-right": {
                    css["bottom"] = 0;
                    css["right"] = 0;
                    break;
                }
                case "notice-bottom-center": {
                    css["bottom"] = 0;
                    css["left"] = css["right"] = 0;
                    css["width"] = "100%";
                    break;
                }
                default: {
                    break;
                }
            }
        }

        if ($options.stack) {
            if ($options.position_class.indexOf("notice-top") !== -1) {
                $($options.appendTo).find('.notice-item-wrapper').each(function () {
                    css["top"] = parseInt($(this).css("top")) + this.offsetHeight + spacing;
                });
            } else if ($options.position_class.indexOf("notice-bottom") !== -1) {
                $($options.appendTo).find('.notice-item-wrapper').each(function () {
                    css["bottom"] = parseInt($(this).css("bottom")) + this.offsetHeight + spacing;
                });
            }
        }

        $element.css(css);

        $element.appendTo($options.appendTo);

        if ($element.fadeIn) {
            $element.fadeIn();
        } else {
            $alert.css({display: 'block', opacity: 1});
        }

        function removeToast() {
            Notice.remove($element);
        }

        if ($options.timeout > 0) {
            setTimeout(removeToast, $options.timeout);
            if ($options.has_progress) {
                $(".notice-progress", $element).animate({"width": "100%"}, $options.timeout);
            }
        }

        $(".notice-close", $element).click(removeToast)

        return $element;
    }

    Notice.remove = function ($element) {
        "use strict";
        if ($element.fadeOut) {
            $element.fadeOut(function () {
                return $element.remove();
            });
        } else {
            $element.remove();
        }
    }

    var success = function (message, options) {
        return Notice(message, 'success', options);
    }
    var warning = function (message, options) {
        return Notice(message, 'warning', options);
    }
    var notice = function (message, options) {
        return Notice(message, 'notice', options);
    }
    var info = function (message, options) {
        return Notice(message, 'info', options);
    }
    var error = function (message, options) {
        return Notice(message, 'error', options);
    }
    exports('notice', {success: success, warning: warning, notice: notice, info: info, error: error});
})

